# urlencode应用场景，多个参数

url = 'https://www.baidu.com/s?wd=周杰伦&sex=男'

# import urllib.parse
#
# data = {
#     'wd': '周杰伦',
#     'sex': '男',
#     'location': '中国台湾省'
# }
#
# a = urllib.parse.urlencode(data)
#
# print(a)

# 获取网页源码
import urllib.request
import urllib.parse

base_url = 'https://www.baidu.com/s?'

data = {
    'wd': '周杰伦',
    'sex': '男',
    'location': '中国台湾省'
}

new_data = urllib.parse.urlencode(data)
# 请求资源路径
url = base_url + new_data

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Cookie': 'BIDUPSID=81BF1A3A8A9E3610F1C66D7DB6D97072; PSTM=1693446505; BD_UPN=12314753; BAIDUID=75BFEEDC587D04E0746F10C21EBCE957:FG=1; H_WISE_SIDS_BFESS=60278_60360_60749; H_PS_PSSID=60278_60840; H_WISE_SIDS=60278_60840; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDSFRCVID=bEDOJexroG30VkQtnpn9U6heoLweG7bTDYrEOwXPsp3LGJLVdfIBEG0Pts1-dEu-S2OOogKK3gOTH4AF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFs5fQRB2Q-XPoO3K82SRvh5-T2h6FAjx7-2M7f5mkf3fbgy4op8P3y0bb2DUA1y4vpKP5A0eTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9Qg-J5lQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjDKj65W3f; ab_sr=1.0.1_YzE4ZWRjZDJkZjY5NWIxZmUyNTAzYTM4NDdiMjNjODk3ODZlYzI0MWEzYjI1ZGY5ZjYxMzgxOThhYTNjODY0ZWE5MWVlMjgzZmNhYWMzM2YwM2UzNzQ1NzhhMjljZDk3MWRkODViZGRhYzY3NjY0ZGZiODRlNGRiZGI4ODk2YWU2OTI4YzlkMWU1Y2MyNDgxYWJmNmQwYWU2NDRiYjA2Ng==; H_PS_645EC=ca76VqBnEP%2FeG5nwXd0yII4vuqKra2xDCGkbKc1BtVGSZkQ7CaLuzTmF41o922jMm91Ix7Q; COOKIE_SESSION=46_0_6_8_6_9_1_0_6_3_1_2_72916_0_3_0_1727491644_0_1727491641%7C9%23408440_17_1727400571%7C9; BAIDUID_BFESS=75BFEEDC587D04E0746F10C21EBCE957:FG=1; BDSFRCVID_BFESS=bEDOJexroG30VkQtnpn9U6heoLweG7bTDYrEOwXPsp3LGJLVdfIBEG0Pts1-dEu-S2OOogKK3gOTH4AF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFs5fQRB2Q-XPoO3K82SRvh5-T2h6FAjx7-2M7f5mkf3fbgy4op8P3y0bb2DUA1y4vpKP5A0eTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9Qg-J5lQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHjDKj65W3f; delPer=0; BD_CK_SAM=1; PSINO=6; BDSVRTM=546; WWW_ST=1727497464388',
    'Referer': 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=15007414_15_dg&wd=%E5%91%A8%E6%9D%B0%E4%BC%A6&oq=%25E5%2591%25A8%25E6%259D%25B0%25E4%25BC%25A6&rsv_pq=b8b04a720006095c&rsv_t=ca76VqBnEP%2FeG5nwXd0yII4vuqKra2xDCGkbKc1BtVGSZkQ7CaLuzTmF41o922jMm91Ix7Q&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
    'X-Requested-With': 'XMLHttpRequest',
    'is_pbs': '%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'is_referer': 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=15007414_15_dg&wd=%E5%91%A8%E6%9D%B0%E4%BC%A6&oq=%25E5%2591%25A8%25E6%259D%25B0%25E4%25BC%25A6&rsv_pq=98d7122e0001b613&rsv_t=4322p2m%2FiRcjVS9TzijyYaDZeRbiJXRvClqy5JPqm5iUjoIWYARHpqoK5k9NhhIAMaKUya8&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&bs=%E5%91%A8%E6%9D%B0%E4%BC%A6',
    'is_xhr': '1',
    'sec-ch-ua': '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
# 请求对象定制

request = urllib.request.Request(url=url, headers=headers)

response = urllib.request.urlopen(request)

content = response.read().decode('utf-8')
print(content)
